package com.netease.xone.widget;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

public class CustomRadioGroup extends LinearLayout implements OnClickListener {

	int mLeftBackGroundDefaultDrawable;
	int mRightBackGroundDefaultDrawable;
	int mMiddleBackGroundDefaultDrawable;
	
	int mLeftBackGroundSelectedDrawable;
	int mRightBackGroundSelectedDrawable;
	int mMiddleBackGroundSelectedDrawable;
	
	int mItemWidth;
	int mItemHeight;
	
	int mBackgroundColor;
	
	int mSegLineColor;
	int mSegLineHeight;
	
	Context mContext;
	
	CustomRadioButton[] mCustomRadioButtons;
	LinearLayout[] mLinearLayouts;
	
	int mIndex = -1;
	
	int mCount;
	
	OnClickListener mOnChangeCheckListener;
	OnClickListener mOnBottomClickListener;
	
	public CustomRadioGroup(Context context) {
		super(context);
		mContext = context;
		setOrientation(LinearLayout.HORIZONTAL);
		setGravity(Gravity.CENTER_HORIZONTAL);
	}

	public void setLeftAndRightAndMiddleBackGroundDrawable(int leftBackGroundDefaultDrawable, int leftBackGroundSelectedDrawable,
			int rightBackGroundDefaultDrawable, int rightBackGroundSelectedDrawable,
			int middleBackGroundDefaultDrawable, int middleBackGroundSelectedDrawable) {
		mLeftBackGroundDefaultDrawable = leftBackGroundDefaultDrawable;
		mLeftBackGroundSelectedDrawable = leftBackGroundSelectedDrawable;
		mRightBackGroundDefaultDrawable = rightBackGroundDefaultDrawable;
		mRightBackGroundSelectedDrawable = rightBackGroundSelectedDrawable;
		mMiddleBackGroundDefaultDrawable = middleBackGroundDefaultDrawable;
		mMiddleBackGroundSelectedDrawable = middleBackGroundSelectedDrawable;
	}
	
	public void setBackgroundColor(int color) {
		mBackgroundColor = color;
		super.setBackgroundColor(color);
	}
	
	public void setItemWidth(int width, int height) {
		mItemWidth = width;
		mItemHeight = height;
	}
	
	public void setSegLineColorAndHeight(int color, int segLineHeight) {
		mSegLineColor = color;
		mSegLineHeight = segLineHeight;
	}
	
	public void setOnCheckedChangeListener(OnClickListener onChangeCheckListener) {
		mOnChangeCheckListener = onChangeCheckListener;
	}
	
	public void setOnBottomClickListener(OnClickListener onChangeCheckListener) {
		mOnBottomClickListener = onChangeCheckListener;
	}
	
	public void addRadioButton(CustomRadioButton[] CustomRadioButtons) {
		mCustomRadioButtons = CustomRadioButtons;
		mCount = CustomRadioButtons.length;
		mLinearLayouts = new LinearLayout[mCount];
		for (int i = 0; i < mCount; i++) {
			mLinearLayouts[i] = new LinearLayout(mContext);
			mLinearLayouts[i].setId(i);
			mLinearLayouts[i].setOnClickListener(this);
			
			setBackground(mIndex, false);

			LinearLayout.LayoutParams lp = null;
			if (mItemWidth == 0) {
				lp = new LinearLayout.LayoutParams(0, mItemHeight, 1);
			} else if (mItemWidth == LinearLayout.LayoutParams.WRAP_CONTENT) {
				lp = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.WRAP_CONTENT, mItemHeight);
			} else {
				lp = new LinearLayout.LayoutParams(mItemWidth, mItemHeight);
			}

			addView(mLinearLayouts[i], lp);

			if (i != mCount - 1) {
				if (mSegLineHeight == -1) {

				} else if (mSegLineHeight == 0) {
					View segLineView = new View(mContext);
					segLineView.setBackgroundColor(mSegLineColor);
					lp = new LinearLayout.LayoutParams(1, mItemHeight);
					addView(segLineView, lp);
				} else {
					LinearLayout segLineView = new LinearLayout(mContext);
					segLineView.setOrientation(LinearLayout.HORIZONTAL);
					segLineView.setBackgroundColor(mBackgroundColor);
					View smallLine = new View(mContext);
					smallLine.setBackgroundColor(mSegLineColor);
					lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, mSegLineHeight);
					lp.gravity = Gravity.CENTER_VERTICAL;
					segLineView.addView(smallLine, lp);
					lp = new LinearLayout.LayoutParams(1, mItemHeight);
					addView(segLineView, lp);
				}
			}
		}

		for (int i = 0; i < CustomRadioButtons.length; i++) {
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			mLinearLayouts[i].addView(CustomRadioButtons[i], lp);
		}
	}

	public void check(int index) {

		if (mIndex != -1) {
			setBackground(mIndex, false);
			
			mCustomRadioButtons[mIndex].setSelected(false);
		}

		mIndex = index;
		setBackground(mIndex, true);
		mCustomRadioButtons[mIndex].setSelected(true);
		
	}
	
	private void check(View v) {
		int index = v.getId();
		
		if (index == mIndex) {
			if (null != mOnBottomClickListener) {
				mOnBottomClickListener.onClick(v);
			}
			return;
		}
		
		check(index);
		
		if (mOnChangeCheckListener != null) {
			mOnChangeCheckListener.onClick(v);
		}
	}
	
	private void setBackground(int index, boolean selected) {
		Drawable drawable = null;
		if (index == 0) {
			if (mLeftBackGroundDefaultDrawable != 0) {
				drawable = getResources().getDrawable(selected ? mLeftBackGroundDefaultDrawable : mLeftBackGroundSelectedDrawable);
			}
		} else if (index == mCount - 1) {
			if (mRightBackGroundDefaultDrawable != 0) {
				drawable = getResources().getDrawable(selected ? mRightBackGroundDefaultDrawable : mRightBackGroundSelectedDrawable);
			}
		} else {
			if (mRightBackGroundDefaultDrawable != 0) {
				drawable = getResources().getDrawable(selected ? mMiddleBackGroundSelectedDrawable : mMiddleBackGroundDefaultDrawable);
			}
		}
		
		if (null != drawable) {
			mLinearLayouts[index].setBackgroundDrawable(drawable);
		}
	}
	
	@Override
	public void onClick(View v) {
		check(v);
	}
	
	public void clear() {
		mContext = null;
		for (int i = 0; i < mCustomRadioButtons.length; i++) {
			mCustomRadioButtons[i].clear();
			mCustomRadioButtons[i] = null;
		}
		mCustomRadioButtons = null;
		
		for (int i = 0; i < mLinearLayouts.length; i++) {
			mLinearLayouts[i] = null;
		}
		mLinearLayouts = null;
		
		mOnChangeCheckListener = null;
		mOnBottomClickListener = null;
	}
}
